package org.ilovecode.display {
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	
	import gs.TweenLite;
	import gs.easing.*;
	
	import org.ilovecode.events.ApartmentEvent;
	import org.ilovecode.events.MenuEvent;
	import org.ilovecode.screen.ScreenGroup;
	import org.ilovecode.screen.ScreenName;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class FloorLayoutBusinessSelling extends FloorLayoutBase {
		
		//{ region Stage elements
		
		/*public var _baloonForSale:FloorLayoutBaseBaloon;
		public var _baloonSold:FloorLayoutBaseBaloon;
		public var _baloonReserved:FloorLayoutBaseBaloon;*/
		public var backButton:SimpleButton;
		private var _floor:FloorBusinessSelling;
		
		//} endregion
		
		private var mode:String;
		
		public function FloorLayoutBusinessSelling(mode:String) {
			super(ScreenName.FLOOR_LAYOUT, ScreenGroup.BUSINESS, "floorLayoutSellingBusiness");
			//this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);
			this.mode = mode;
		}
		
		override protected function construct():void
		{
			super.construct();
			
			_floor = new FloorBusinessSelling(mode);
			_floor.mouseEnabled = false;
			this.addChild(_floor);
		}
		
		
		override protected function initUI():void
		{
			super.initUI();
			
			_floor.x = 289;
			_floor.y = 14;
		}
		
		override protected function addListeners():void
		{
			super.addListeners();
			backButton.addEventListener(MouseEvent.CLICK, backButtonClick);
			_floor.addEventListener(ApartmentEvent.CLICK, roomClick);
			_floor.addEventListener(ApartmentEvent.MOUSE_OVER, roomMouseOver);
			_floor.addEventListener(ApartmentEvent.MOUSE_OUT, roomMouseOut);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			backButton.removeEventListener(MouseEvent.CLICK, backButtonClick);
			_floor.removeEventListener(ApartmentEvent.CLICK, roomClick);
			_floor.removeEventListener(ApartmentEvent.MOUSE_OVER, roomMouseOver);
			_floor.removeEventListener(ApartmentEvent.MOUSE_OUT, roomMouseOut);
		}
		
		
		/*private function addedToStage(event:Event):void {
			//super.setBaloons(baloonForSale, baloonSold, baloonReserved);
			init();
		}
		
		private function init():void {
			initBaloon(_baloonForSale);
			initBaloon(_baloonReserved);
			initBaloon(_baloonSold);
			
			configRooms(floor);
			initListeners();
		}
		
		private function initListeners():void {
			backButton.addEventListener(MouseEvent.CLICK, backButtonClick);
		}
		
		private function backButtonClick(event:MouseEvent):void {
			this.dispatchEvent(new MenuEvent(MenuEvent.PREVIOUS, ""));
		}
		
		private function configRooms(floor:FloorBusinessSelling):void {
			floor.init(mode);
			floor.mouseEnabled = false;
			floor.addEventListener(ApartmentEvent.CLICK, roomClick);
			floor.addEventListener(ApartmentEvent.MOUSE_OVER, roomMouseOver);
			floor.addEventListener(ApartmentEvent.MOUSE_OUT, roomMouseOut);
		}*/
		
		private function backButtonClick(event:MouseEvent):void {
			//this.dispatchEvent(new MenuEvent(MenuEvent.PREVIOUS, ""));
			controller.showScreen(ScreenName.RENDER);
		}
		
		override protected function roomMouseOver(event:ApartmentEvent):void {
			var room:MovieClip = null;
			var tweenFlag:Boolean = false;
			
			switch(String(event.item)) {
				case "vrtic":
					if (mode != "kindergarten") {
						room = _floor.getRoomByName("vrtic");
					}
					break;
				case "E_0_010":
				case "E_0_012":
					room = _floor.getRoomByName(String(event.item));
					if (mode != "catering") {
						tweenFlag = true;
					}
					break;
				default:
					room = _floor.getRoomByName(String(event.item));
					if (mode != "shop") {
						tweenFlag = true;
					}
					break;
			}
			
			if (room != null) {
				if (String(event.item) == "vrtic") {
					TweenLite.to(room, duration, { alpha:0.6 } );
				} else {
					if (tweenFlag) {
						TweenLite.to(FloorLayoutBaseRoom(room).instance, duration, { alpha:0.6 } );
					}
					toggleBaloon(FloorLayoutBaseRoom(room), true);
				}
			}
		}
		
		override protected function roomMouseOut(event:ApartmentEvent):void {
			var room:MovieClip = null;
			var tweenFlag:Boolean = false;
			
			switch(String(event.item)) {
				case "vrtic":
					if (mode != "kindergarten") {
						room = _floor.getRoomByName("vrtic");
					}
					break;
				case "E_0_010":
				case "E_0_012":
					room = _floor.getRoomByName(String(event.item));
					if (mode != "catering") {
						tweenFlag = true;
					}
					break;
				default:
					room = _floor.getRoomByName(String(event.item));
					if (mode != "shop") {
						tweenFlag = true;
					}
					break;
			}
			
			if (room != null) {
				if (String(event.item) == "vrtic") {
					TweenLite.to(room, duration, { alpha:0 } );
				} else {
					if (tweenFlag) {
						TweenLite.to(FloorLayoutBaseRoom(room).instance, duration, { alpha:0 } );
					}
					toggleBaloon(FloorLayoutBaseRoom(room), false);
				}
			}
		}
		
		override protected function roomClick(event:ApartmentEvent):void {
			if (String(event.item) != "vrtic") {
				var room:FloorLayoutBaseRoom = FloorLayoutBaseRoom(_floor.getRoomByName(String(event.item)));
				if (room != null) {
					var o:Object = new Object();
					o.roomID = room.id;
					//this.dispatchEvent(new ApartmentEvent(ApartmentEvent.CLICK, o));
					controller.showApartment(int(room.id), 0, "floorLayoutBusinessSelling", true);
				}
			}
		}
		
		public function set sellingMode(value:String):void {
			mode = value;
		}
		
		public function get sellingMode():String {
			return mode;
		}
		
	}

}